package library.view;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import library.pojo.User;
import library.service.LibraryService;

public class UpdateMessageDialog extends JDialog {
	private User user;
	private JTextField usernameTextField;
	private JTextField passwordTextField;
	private JTextField newPasswordTextField;
	private JTextField nameTextField;
	private JTextField phoneTextField;
	private JTextField typeTextField;
	private TotalFrameSystem t1;
	private TotalFrameCommon t2;
	private TotalFrameBook t3;

	public UpdateMessageDialog(String title, User user, TotalFrameSystem t1,TotalFrameCommon t2,TotalFrameBook t3) {
		ImageIcon icon = new ImageIcon(MainJTPane.class.getResource("/title.png"));
		this.setIconImage(icon.getImage());
		this.t1 = t1;
		this.t2 = t2;
		this.t3 = t3;	
		this.user = user;
		this.setTitle(title);
		this.setBounds(100, 100, 440, 610);
		this.add(getUserPanel());
		this.setModal(true);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.validate();
	}

	private JPanel getUserPanel() {
		JPanel userPanel = new JPanel();
		userPanel.setBounds(100, 100, 440, 610);
		userPanel.setLayout(null);

		JLabel registerLabel = new JLabel("修改信息");
		registerLabel.setIcon(new ImageIcon("res/update.png"));
		registerLabel.setFont(new Font("宋体", Font.PLAIN, 40));
		registerLabel.setBounds(117, 26, 226, 59);
		this.add(registerLabel);

		JLabel userNameLabel = new JLabel("  用户名：");
		userNameLabel.setFont(new Font("宋体", Font.PLAIN, 20));
		userNameLabel.setBounds(25, 116, 112, 24);
		userPanel.add(userNameLabel);

		JLabel passwordLabel = new JLabel("  旧密码：");
		passwordLabel.setFont(new Font("宋体", Font.PLAIN, 20));
		passwordLabel.setBounds(25, 176, 112, 24);
		userPanel.add(passwordLabel);

		JLabel confirmPasswordLabel = new JLabel("  新密码：");
		confirmPasswordLabel.setFont(new Font("宋体", Font.PLAIN, 20));
		confirmPasswordLabel.setBounds(25, 236, 112, 24);
		userPanel.add(confirmPasswordLabel);

		JLabel nameLabel = new JLabel("    姓名：");
		nameLabel.setFont(new Font("宋体", Font.PLAIN, 20));
		nameLabel.setBounds(25, 296, 112, 24);
		userPanel.add(nameLabel);

		JLabel phoneLabel = new JLabel("联系电话：");
		phoneLabel.setFont(new Font("宋体", Font.PLAIN, 20));
		phoneLabel.setBounds(25, 356, 112, 24);
		userPanel.add(phoneLabel);

		JLabel typeLabel = new JLabel("用户类型：");
		typeLabel.setFont(new Font("宋体", Font.PLAIN, 20));
		typeLabel.setBounds(25, 416, 112, 24);
		userPanel.add(typeLabel);

		usernameTextField = new JTextField();
		usernameTextField.setBounds(151, 119, 215, 22);
		usernameTextField.setEditable(false);
		usernameTextField.setText(user.getUsername());
		userPanel.add(usernameTextField);

		passwordTextField = new JTextField();
		passwordTextField.setColumns(10);
		passwordTextField.setBounds(151, 179, 215, 22);
		userPanel.add(passwordTextField);

		newPasswordTextField = new JTextField();
		newPasswordTextField.setColumns(10);
		newPasswordTextField.setBounds(151, 239, 215, 22);
		userPanel.add(newPasswordTextField);

		nameTextField = new JTextField();
		nameTextField.setText(user.getName());
		nameTextField.setBounds(151, 299, 215, 22);
		userPanel.add(nameTextField);

		phoneTextField = new JTextField();
		phoneTextField.setText(user.getPhone());
		phoneTextField.setBounds(151, 359, 215, 22);
		userPanel.add(phoneTextField);

		typeTextField = new JTextField();
		typeTextField.setEditable(false);
		typeTextField.setText(user.getType());
		typeTextField.setBounds(151, 419, 215, 22);
		userPanel.add(typeTextField);

		JButton updateButton = new JButton("确认修改");
		updateButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updateActionPerformed(e);
			}
		});
		updateButton.setBounds(138, 488, 167, 39);
		userPanel.add(updateButton);

		userPanel.setVisible(true);
		userPanel.validate();
		return userPanel;
	}

	protected void updateActionPerformed(ActionEvent e) {
		if (this.isEmpty(passwordTextField.getText())) {
			JOptionPane.showMessageDialog(null, "旧密码不能为空！");
			return;
		}
		if (this.isEmpty(newPasswordTextField.getText())) {
			JOptionPane.showMessageDialog(null, "新密码不能为空！");
			return;
		}
		if (!passwordTextField.getText().equals(user.getPassword())) {
			JOptionPane.showMessageDialog(null, "旧密码错误，请重新输入！");
			return;
		}
		if (newPasswordTextField.getText().length() <3 ) {
			JOptionPane.showMessageDialog(null, "密码长度不得小于3位！");
			return;
		}
		if (this.isEmpty(nameTextField.getText())) {
			JOptionPane.showMessageDialog(null, "姓名不能为空！");
			return;
		}
		if (this.isEmpty(phoneTextField.getText())) {
			JOptionPane.showMessageDialog(null, "联系电话不能为空！");
			return;
		}
		if (!phoneTextField.getText().matches("[0-9]{0,11}") || phoneTextField.getText().length() != 11) {
			JOptionPane.showMessageDialog(null, "请输入正确的手机号！");
			return;
		}
		LibraryService service = new LibraryService();
		User users = new User(usernameTextField.getText(), newPasswordTextField.getText(), nameTextField.getText(),
				typeTextField.getText(), phoneTextField.getText());
		service.updateUser(users);
		this.dispose();
		JOptionPane.showMessageDialog(null, "修改成功！");
		JOptionPane.showMessageDialog(null, "请重新登录！");
		if(this.t1!=null) {
			t1.dispose();
		}
		if(this.t2!=null) {
			t2.dispose();
		}
		if(this.t3!=null) {
			t3.dispose();
		}
		LoginFrame login = new LoginFrame();
	}

	/**
	 * 判断是否为空
	 */
	private boolean isEmpty(String str) {
		if (str == null || "".equals(str.trim())) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 判断两次输入的密码是否一致
	 */
	private boolean isNotSame(String str1, String str2) {
		if (str1.equals(str2)) {
			return false;
		} else {
			return true;
		}
	}
}
